package com.example.tool.erp.demo;

import com.example.tool.erp.sdk.Client;
import com.example.tool.erp.sdk.Pager;
import com.example.tool.erp.sdk.WdtErpException;
import com.example.tool.erp.sdk.api.wms.stockout.StockoutAPI;
import com.example.tool.erp.sdk.api.wms.stockout.dto.*;
import com.example.tool.erp.sdk.impl.ApiFactory;
import com.example.tool.erp.sdk.impl.DefaultClient;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

public class Stockout
{
//	public static void main(String[] args) throws WdtErpException
//	{
//		// Client client = DefaultClient.get("wdtapi3", "test", "test");
//		Client client = DefaultClient.get("wdterp30", "http://172.172.1.62:30000/", "xyx_api",
//				"e8866c1681dacc9488a19c89991791b8:8f5800dab84b7a8d614e92cba739032c");
//		StockoutAPI stockoutAPI = ApiFactory.get(client, StockoutAPI.class);
////		testCreateOtherOutOrder(stockoutAPI);
//		// searchProcessStockout(stockoutAPI);
////		 querySalesStockout(stockoutAPI);
//		// testCreateTransferOrder(stockoutAPI);
////		testStockoutSearch(stockoutAPI);
////		testPurchaseReturnStockoutSearch(stockoutAPI);
////		testCreatePurchaseReturnStockout(stockoutAPI);
////		testTransferStockoutQueryWithDetail(stockoutAPI);
////		testCancelTransferStockoutOrder(stockoutAPI);
////		testPickListOverViewForApi(stockoutAPI);
//		testSearchPack(stockoutAPI);
//	}

	private static void querySalesStockout(StockoutAPI stockoutApi) throws WdtErpException
	{
		int pageSize = 5;

		SalesStockoutRequest request = new SalesStockoutRequest();
		// request.setStockoutNo("CK2019112057");
//		request.setWarehouseNo("1001");
		// request.setStatusType(SalesStockoutRequest.STATUS_TYPE_CONSIGNED);
		request.setStartTime("2020-10-10 00:00:00");
		request.setEndTime("2020-10-12 00:00:00");

		//获取过去某个时间段的数据, 先获取总条数, 之后倒序翻页获取数据
		Pager pager = new Pager(pageSize, 0, true);
		SalesStockoutResponse response = stockoutApi.querySales(request, pager);
		Integer totalCount = response.getTotal();
		if (totalCount == null || totalCount == 0)
		{
			System.out.println("没有符合条件的结果.");
			return;
		}
		int totalPage = (totalCount % pageSize == 0 ? totalCount / pageSize : totalCount / pageSize + 1) - 1;
		pager.setCalcTotal(false);
		System.out.println("total_page: " + totalPage);

		// 结果仅有一页
		if (totalPage == 0)
		{
			System.out.println("我在这里处理数据");
		}
		else
		{
			for (int i = totalPage; i >= 0; i--)// 从后向前翻页
			{
				pager.setPageNo(i);
				System.out.print("pager: page_size:" + pageSize + "  page_no: " + i + "  ");
				response = stockoutApi.querySales(request, pager);

				String out = response.getOrderList().stream().map(SalesStockoutResponse.OrderInfoDto::getTradeNo)
						.collect(Collectors.joining(","));
				System.out.println("接收到的单据, 我需要做去重: " + out);
			}
		}
	}

	private static void searchProcessStockout(StockoutAPI stockoutApi)
	{
		ProcessStockoutRequest request = new ProcessStockoutRequest();
		request.setProcessNo("PS2020062202");
		ProcessStockoutResponse response = stockoutApi.searchProcess(request, new Pager(50, 0, true));

	}

	private static void testCreateOtherOutOrder(StockoutAPI stockoutAPI)
	{
		CreateOtherStockoutRequest request = new CreateOtherStockoutRequest();
		CreateOtherStockoutRequest.GoodsList inrequest1 = new CreateOtherStockoutRequest.GoodsList();
		// CreateOtherStockoutRequest.GoodsList inrequest2 = new
		// CreateOtherStockoutRequest.GoodsList();
		request.setWarehouseNo("1001");
		request.setOuterNo("CG201911286258");
		request.setRemark("测试1");
		request.setReason("1");
		inrequest1.setSpecNo("PC_2016");
		inrequest1.setPositionNo("J-3");
		inrequest1.setNum(BigDecimal.valueOf(3));
		List<CreateOtherStockoutRequest.GoodsList> list = new ArrayList<>();
		list.add(inrequest1);
		request.setGoodsList(list);

		CreateOtherStockoutResponse response = stockoutAPI.createOtherOutOrder(request);
		response.toString();
	}

	private static void testCreateTransferOrder(StockoutAPI stockoutAPI)
	{
		CreateTransferStockoutRequest.orderInfoDto orderInfo = new CreateTransferStockoutRequest.orderInfoDto();
		CreateTransferStockoutRequest.detailDto detailDto1 = new CreateTransferStockoutRequest.detailDto();

		orderInfo.setSrcOrderNo("TF202003020004");
		orderInfo.setWarehouseNo("lz");
		orderInfo.setRemark("调拨出库单新建");
		detailDto1.setNum("1");
		detailDto1.setSpecNo("lz41");
		detailDto1.setUnitName("lz1");

		List<CreateTransferStockoutRequest.detailDto> detailList = new ArrayList<>();
		detailList.add(detailDto1);

		CreateTransferStockoutResponse response = stockoutAPI.createTransferOrder(orderInfo, detailList, true);
		response.toString();
	}

	private static void testStockoutSearch(StockoutAPI api) throws WdtErpException
	{
		int pageSize = 5;
		Pager pager = new Pager(pageSize, 0, true);
		StockoutSearchRequest request = new StockoutSearchRequest();
		//		request.setStartTime("2023-01-11 17:18:21");
		//		request.setEndTime("2023-01-11 18:18:21");
		//		request.setOrderType((byte)1);
		request.setStockoutNo("CK2023041715");

		StockoutSearchResponse response = api.search(request, pager);
		Integer total = response.getTotal();
		if (null == total || pageSize >= total)
		{
			System.out.println("处理数据");
			return;
		}

		int totalPage = (total % pageSize == 0 ? total / pageSize : total / pageSize + 1) - 1;
		pager.setCalcTotal(false); //后续翻页不需要计算总条数, 可以大大减少请求时间
		for (int i = totalPage; i >= 0; i--)// 从后向前翻页
		{
			pager.setPageNo(i);
			System.out.print("pager: page_size:" + pageSize + "  page_no: " + i + "  ");
			response = api.search(request, pager);
			System.out.println(response.getOrderList());
		}
	}

	private static void testPurchaseReturnStockoutSearch(StockoutAPI api) throws WdtErpException
	{
		int pageSize = 5;
		Pager pager = new Pager(pageSize, 0, true);
		PurchaseReturnStockOutSearchRequest request = new PurchaseReturnStockOutSearchRequest();
		request.setStartTime("2022-03-01");
		request.setEndTime("2022-03-31");

		PurchaseReturnStockOutSearchResponse response = api.searchPurchaseReturn(request, pager);
		Integer total = response.getTotal();
		if (null == total || pageSize >= total)
		{
			System.out.println("处理数据");
			return;
		}

		int totalPage = (total % pageSize == 0 ? total / pageSize : total / pageSize + 1) - 1;
		pager.setCalcTotal(false); //后续翻页不需要计算总条数, 可以大大减少请求时间
		for (int i = totalPage; i >= 0; i--)// 从后向前翻页
		{
			pager.setPageNo(i);
			System.out.print("pager: page_size:" + pageSize + "  page_no: " + i + "  ");
			response = api.searchPurchaseReturn(request, pager);
			System.out.println(response.getOrderList());
		}
	}

	private static void testCreatePurchaseReturnStockout(StockoutAPI api) throws WdtErpException
	{
		PurchaseReturnStockoutCreateRequest.OrderInfo orderInfo = new PurchaseReturnStockoutCreateRequest.OrderInfo();
		orderInfo.setReturnNo("CR202207070004");
		orderInfo.setWarehouseNo("xyx");

		List<PurchaseReturnStockoutCreateRequest.Detail> detailList = new ArrayList<>();

		PurchaseReturnStockoutCreateRequest.Detail detail = new PurchaseReturnStockoutCreateRequest.Detail();
		detailList.add(detail);
		detail.setSpecNo("TEST");
		detail.setNum(new BigDecimal("2"));

		PurchaseReturnStockoutCreateResponse response = api.createPurchaseReturn(orderInfo, detailList);
		System.out.println("单号: "+response.getMessage());
	}

	private static void testTransferStockoutQueryWithDetail(StockoutAPI api) throws WdtErpException
	{
		int pageSize = 5;
		Pager pager = new Pager(pageSize, 0, true);
		TransferStockoutQueryWithDetailRequest request = new TransferStockoutQueryWithDetailRequest();
		request.setStartTime("2022-03-01");
		request.setEndTime("2022-03-31");

		TransferStockoutQueryWithDetailResponse response = api.queryTransferWithDetail(request, pager);
		Integer total = response.getTotal();
		if (null == total || pageSize >= total)
		{
			System.out.println("处理数据");
			return;
		}

		int totalPage = (total % pageSize == 0 ? total / pageSize : total / pageSize + 1) - 1;
		pager.setCalcTotal(false); //后续翻页不需要计算总条数, 可以大大减少请求时间
		for (int i = totalPage; i >= 0; i--)// 从后向前翻页
		{
			pager.setPageNo(i);
			System.out.print("pager: page_size:" + pageSize + "  page_no: " + i + "  ");
			response = api.queryTransferWithDetail(request, pager);
			System.out.println(response.getOrderList());
		}
	}

	private static void testCancelTransferStockoutOrder(StockoutAPI api) throws WdtErpException
	{
		api.cancelTransferStockoutOrder("CK2023052418");
	}

	private static void testPickListOverViewForApi(StockoutAPI api) throws WdtErpException
	{
		PickListOverViewForApiResponse response = api.pickListOverviewForApi("FJ2303230001");

		System.out.println(response.getData());
	}

	private static void testSearchPack(StockoutAPI api) throws WdtErpException
	{
		int pageSize = 5;
		Pager pager = new Pager(pageSize, 0, true);
		PackSearchRequest request = new PackSearchRequest();
		request.setStartTime("2023-01-01");
		request.setEndTime("2023-01-31");

		PackSearchResponse response = api.searchPack(request, pager);
		Integer total = response.getTotal();
		if (null == total || pageSize >= total)
		{
			System.out.println("处理数据");
			return;
		}

		int totalPage = (total % pageSize == 0 ? total / pageSize : total / pageSize + 1) - 1;
		pager.setCalcTotal(false); //后续翻页不需要计算总条数, 可以大大减少请求时间
		for (int i = totalPage; i >= 0; i--)// 从后向前翻页
		{
			pager.setPageNo(i);
			System.out.print("pager: page_size:" + pageSize + "  page_no: " + i + "  ");
			response = api.searchPack(request, pager);
			System.out.println(response.getOrderList());
		}
	}
}
